package org.semanticflash.rdf.graph.redirection.provider
{
	
	
	import com.bigflexlib.ds.container.zet.IOSet;
	import com.bigflexlib.ds.container.zet.IOWSet;
	import com.bigflexlib.ds.container.zet.impl.owSet;
	import com.bigflexlib.ds.op.SetMappingOp;
	import com.bigflexlib.utils.IOperator;
	import com.bigflexlib.x.flash.events.EventListenerSpec;
	
	import flash.events.Event;
	
	import org.semanticflash.rdf.graph.redirection.RDFRedirectionContext;
	import org.semanticflash.rdf.graph.redirection.RDFTripleRedirection;
	import org.semanticflash.rdf.triple.IRDFIndexedTripleProvider;
	import org.semanticflash.rdf.triple.RDFTriple;



	/**
	 * 
	 * A GraphView is a filtered set of the statements contained in a graph.
	 * It operates over one or more index entries.
	 * 
	 * This is where IRI redirections take place. The RDFGraphView connects and disconnects
	 * from the graph every time a relevant IRI redirection happens
	 * 
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */
	public class RDFRedirectedTripleView
	{
		

		////////////////////////////////////////////////////
		// triples 
		////////////////////////////////////////////////////
		
		private var _triples:IOWSet;
		/**
		 * 
		 * A filtered subset of the triples on the graph.
		 * 
		 * @return 
		 * 
		 */
		public function get triples( ):IOSet
		{
			return _triples;
		}



		////////////////////////////////////////////////////
		// provider
		////////////////////////////////////////////////////

		private var provider:IRDFIndexedTripleProvider;


		////////////////////////////////////////////////////
		// mask
		////////////////////////////////////////////////////
		
		private var mask:RDFTriple;
		
		
		////////////////////////////////////////////////////
		// context
		////////////////////////////////////////////////////
		
		private var context:RDFRedirectionContext;

		
		
		////////////////////////////////////////////////////
		// redirectedMask
		////////////////////////////////////////////////////

		private var redirectedMask:RDFTriple;



		////////////////////////////////////////////////////
		// tripleRedirection
		////////////////////////////////////////////////////

		/**
		* we keep a local copy of the triple redirection for our tripleMask.
		*/		
		private var tripleRedirection:RDFTripleRedirection;
		
		private var tripleRedirectionListenerSpec:EventListenerSpec = new EventListenerSpec( Event.CHANGE, onRedirectionChange, false, 0, false );


		

		////////////////////////////////////////////////////
		// etc...
		////////////////////////////////////////////////////



		private var mappingOperator:IOperator;



		////////////////////////////////////////////////////
		// constructor
		////////////////////////////////////////////////////

		/**
		 * 
		 * 
		 * 
		 * 
		 * @param graph
		 * @param tripleMask
		 * 
		 */		
		public function RDFRedirectedTripleView( mask:RDFTriple, context:RDFRedirectionContext, provider:IRDFIndexedTripleProvider )
		{
		
			this.mask = mask;
			
			this.context = context;
			
			this.provider = provider;

			
			_triples = owSet( );
			
			
			// create a local copy of the triple redirection
			// and listen to its events

			tripleRedirection = context.getTripleRedirection( mask );
			tripleRedirectionListenerSpec.addToDispatcher( tripleRedirection );


			refresh( );

		}
		
		





		/**
		 * 
		 * 
		 */		
		private function refresh( ):void
		{
			
			if ( mappingOperator ) // dispose the previous mapping
				mappingOperator.dispose( );
			

			// get the redirected mask
			redirectedMask = tripleRedirection.target;
			
			// get the non redirected set ( from the provider )
			var t:IOSet = provider.getTriples( redirectedMask );
			

			// create a new pass-thru mapping between sets
			mappingOperator = new SetMappingOp( t, _triples );					

		}




		
		
		private function onRedirectionChange( event:Event ):void
		{
			refresh( );
		}
		
		
		
		

	}
	
	

}


